﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DBQLTV;
namespace QuanLyThuVien
{
    public partial class frm_XoaSach : Form
    {
        private frmMain mainForm = null;
        private SachBUS sachBUS = new SachBUS();
        public frm_XoaSach(Form callingForm)
        {
            mainForm = callingForm as frmMain;
            InitializeComponent();
        }
        private void frm_XoaSach_Load(object sender, EventArgs e)
        {
            List<SACH> listSach = sachBUS.GetListSach();
            foreach (SACH s in listSach)
            {
                comboBox_maSach.Items.Add(s.MaSach);
            }
        
        }
        private void UpdateThongTin()
        {
            int maSach = int.Parse(comboBox_maSach.Text);
            tb_tenSach.Text = sachBUS.GetDauSach(sachBUS.GetSach(maSach)).TenSach;
            DAUSACH dausach = new DauSachBUS().GetDauSach(maSach);
            tb_tenSach.Text = dausach.TenSach;
            tb_theLoai.Text = sachBUS.GetTheLoaiSach(maSach).TenTheLoaiSach;
            List<string> listTacGia = sachBUS.GetListTenTacGia(maSach);
            string temp = "";
            foreach (string ten in listTacGia)
            {
                temp += ten + " , ";
            }
            tb_tacGia.Text = temp;
        }
        private void comboBox_maSach_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (!comboBox_maSach.Items.Contains(comboBox_maSach.Text))
                    MessageBox.Show("Ma sach khong ton tai!");
                else UpdateThongTin();
            }
        }
        private void button_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void button_luu_Click(object sender, EventArgs e)
        {
            if (comboBox_maSach.Text == "")
                MessageBox.Show("Chưa nhập mã sách");
            else
            {
                int maSach = int.Parse(comboBox_maSach.Text);
                if (!comboBox_maSach.Items.Contains(maSach))
                    MessageBox.Show("Ma sach khong ton tai!");
                else
                {
                    if (sachBUS.DeleteSach(maSach))
                    {
                        MessageBox.Show("Xóa thành công");
                        mainForm.LoadDataGrid_DSSach();
                        this.Close();
                    }
                    else MessageBox.Show("Có (nhiều) cuốn sách trong loại sách này đang được mượn");
                }
            }
        }
        private void comboBox_maSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateThongTin();
        }  
    }
}
